// src/stores/auth.js
import { defineStore } from 'pinia';
import { loginApi, refreshTokenApi } from '@/api/auth';

export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: null
  }),
  actions: {
    async login(formData) {
      const res = await loginApi(formData);
      this.token = res.data.token;
      localStorage.setItem('token', this.token);
    },
    async logout() {
      this.token = '';
      localStorage.removeItem('token');
    },
    // Token刷新逻辑
    async refreshToken() {
      try {
        const res = await refreshTokenApi();
        this.token = res.data.token;
        localStorage.setItem('token', this.token);
        return true;
      } catch (e) {
        this.logout();
        return false;
      }
    }
  }
});